package com.dts.evaluate.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextClock;
import android.widget.TextView;

import com.dts.evaluate.R;

/**
 * Created by user on 2016/9/19.
 */
public class TitleView extends RelativeLayout implements View.OnClickListener{
    private TextView mTextView;
    private TextClock mTextClock;
    private TextView mBackTextView;
    private TextView mOtherTextView;

    private int visiblity = GONE;
    private int otherVisiblity = GONE;
    private String titleText = "";
    private String backText = "";
    private String otherText = "";

    public TitleView(Context context) {
        super(context);
        init(context,null,0);
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs,0);
    }

    public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs,defStyleAttr);
    }

    private void init(Context context,AttributeSet attrs,int defStyleAttr){
        LayoutInflater.from(context).inflate(R.layout.title_view_layout,this);
        mTextView = (TextView) findViewById(R.id.title_textview);
        mTextClock = (TextClock) findViewById(R.id.time_textclock);
        mBackTextView = (TextView) findViewById(R.id.back_textview);
        mOtherTextView = (TextView) findViewById(R.id.other_textview);

        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.TitleView,defStyleAttr,0);
        int N = a.getIndexCount();
        for (int i = 0;i<N;i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.TitleView_timeVisiblity:
                    visiblity = a.getInt(attr,GONE);
                    break;
                case R.styleable.TitleView_otherVisiblity:
                    otherVisiblity = a.getInt(attr,GONE);
                    break;
                case R.styleable.TitleView_otherText:
                    otherText = a.getString(attr);
                    break;
                case R.styleable.TitleView_titleText:
                    titleText = a.getString(attr);
                    break;
                case R.styleable.TitleView_backText:
                    backText = a.getString(attr);
                    break;

            }
        }
        a.recycle();

        mTextView.setText(titleText);
        mBackTextView.setText(backText);
        mOtherTextView.setText(otherText);
        mOtherTextView.setOnClickListener(this);
        mBackTextView.setOnClickListener(this);
        switch (visiblity){
            case GONE:
                mTextClock.setVisibility(GONE);
                break;
            case INVISIBLE:
                mTextClock.setVisibility(INVISIBLE);
                break;
            case VISIBLE:
                mTextClock.setVisibility(VISIBLE);
                break;
        }
        switch (otherVisiblity){
            case GONE:
                mOtherTextView.setVisibility(GONE);
                break;
            case INVISIBLE:
                mOtherTextView.setVisibility(INVISIBLE);
                break;
            case VISIBLE:
                mOtherTextView.setVisibility(VISIBLE);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        if(mOnBackClikListener != null){
            mOnBackClikListener.onTitleViewClik(v);
        }
    }

    private OnBackClikListener mOnBackClikListener;

    public void setOnBackClickListener(OnBackClikListener onBackClickListener){
        this.mOnBackClikListener = onBackClickListener;
    }

    /**
     * 返回按钮的点击回调
     */
    public interface OnBackClikListener{
        void onTitleViewClik(View v);
    }
}
